package lion.blog.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import lion.blog.model.BlogCatelog;
import lion.dev.web.paging.Paging;
import lion.framework.core.web.path.PathVar;
import lombok.Setter;

/**
 * 
 * 文章列表页分页条
 * 
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年10月31日
 */
public class ArticlePagebarTag extends TagSupport {

	private static final long serialVersionUID = 766747788030804384L;

	private static final String PAGER_URL_PATTERN = "<a href=\"%s\">%s</a>";
	private static final String CURRENT_PAGER_URL_PATTERN = "<a class=\"current\">%s</a>";
	private static final String BLOG_LIST_PAGER_TEXT = "blog_list_pager_text";

	@Setter
	private int segs = 4;

	private String pattern;
	private Paging paging;

	@Override
	public int doStartTag() throws JspException {

		HttpServletRequest hsr = (HttpServletRequest) this.pageContext.getRequest();

		this.paging = (Paging) hsr.getAttribute("paging");

		BlogCatelog catelog = (BlogCatelog) hsr.getAttribute("catelog");

		if (catelog == null) { //
			pattern = hsr.getContextPath() + "/" + PathVar.getVar("blog") + "/p{page}";
		} else {// 栏目列表页
			pattern = hsr.getContextPath() + "/" + PathVar.getVar("blog") + "/" + catelog.getCode() + "/p{page}";
		}
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {

		if (this.paging == null) { return EVAL_PAGE; }

		// only one page
		if (this.paging.getPageCount() <= 1) {
			out(getPageLink("1", 1));
			return EVAL_PAGE;
		}

		// cached pager text
		String pagerCache = (String) pageContext.getAttribute(BLOG_LIST_PAGER_TEXT);
		if (pagerCache != null) {
			out(pagerCache);
			return EVAL_PAGE;
		}

		StringBuilder buffer = new StringBuilder();
		int currentSeg = (int) Math.floor((this.paging.getPageNumber() - 2.0) / segs);
		if (currentSeg < 0) {
			currentSeg = 0;
		}
		int segStartPage = segs * currentSeg + 2;

		// first
		if (paging.getPageCount() > 0) {
			buffer.append(getPageLink("1", 1));
		}

		// pre seg ...
		if (currentSeg > 0) {
			buffer.append(getPageLink("...", segStartPage - 1));
		}

		// segs
		for (int i = 0, j = segStartPage; i < segs && j < paging.getPageCount(); i++, j++) {
			buffer.append(getPageLink(j + "", j));
		}

		// hasnext seg?
		if (segStartPage + segs < paging.getPageCount()) {
			buffer.append(getPageLink("...", segStartPage + segs));
		}

		// last
		if (paging.getPageCount() > 1) {
			buffer.append(getPageLink(paging.getPageCount() + "", paging.getPageCount()));
		}

		String text = buffer.toString();
		pageContext.setAttribute(BLOG_LIST_PAGER_TEXT, text);

		out(buffer.toString());

		return super.doEndTag();
	}

	private void out(String str) {

		try {
			pageContext.getOut().write(str);
		} catch (IOException e) {
		}
	}

	private String getPageLink(String text, int pageNum) {

		String url = this.pattern.replace("{page}", pageNum + "");

		if (pageNum == this.paging.getPageNumber()) { return String.format(CURRENT_PAGER_URL_PATTERN, text); }

		return String.format(PAGER_URL_PATTERN, url, text);
	}
}
